/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.cicp.util.pool;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.log4j.Logger;

/**
 * <p>
 * Description: JMX的连接池   
 * [TIP]: 暂时不用   需多测试   如重启weblogic 看可否继续采集
 * </p>
 *
 * @author wangyazhou
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-10-26 上午9:56:08          wangyazhou        1.0         To create
 * </p>
 *
 * @since     
 * @see     
 */
public class JMXConnPool {

    private static final Logger logger = Logger.getLogger(JMXConnPool.class);

    private static JMXConnPool instance = null;

    private Object _lock = new Object();

    private int outTime = 3 * 60 * 1000;

    private int _defaultSize = 2;

    private Map<String, ObjectPool> map = new HashMap<String, ObjectPool>();

    private JMXConnPool() {

    }

    public static synchronized JMXConnPool getInstance() {
        if (instance == null) {
            instance = new JMXConnPool();
        }
        return instance;
    }

    public BSMJMXConnector getConnection(String type, String host, int port, String user, String pwd) throws Exception {
        ObjectPool pool = map.get(host + port);
        if (pool == null) {
            synchronized (_lock) {
                if (pool == null) {
                    JMXPoolableObject factory = new JMXPoolableObject(type, host, port, user, pwd);
                    GenericObjectPool.Config config = new GenericObjectPool.Config();
                    config.lifo = false;
                    config.maxActive = _defaultSize;
                    config.maxIdle = _defaultSize;
                    config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_BLOCK;
                    config.minIdle = 1;
                    config.testOnReturn = true;
                    config.maxWait = outTime;
                    pool = new GenericObjectPool(factory, config);
                    map.put(host, pool);
                }
            }
        }

        return (BSMJMXConnector) pool.borrowObject();
    }

    public void free(String host, int port, BSMJMXConnector conn) {
        ObjectPool pool = map.get(host + port);
        try {
            if (pool != null) {
                pool.returnObject(conn);
            }
        } catch (Exception e) {
            logger.error(e);
        }
    }

    public void updateUser(String ip, int port) {
        ObjectPool pool = map.get(ip + port);
        try {
            logger.info("[TIP]\t重建连接池...");
            pool.close();
            map.put(ip + port, null);
        } catch (Exception e) {
            logger.error(e);
        }
    }

}
